package com.ta.init;

import com.ta.mapper.ProductMapper;
import com.ta.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
@Component
public class InitScheduleRun implements ApplicationRunner {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ProductMapper productMapper;
    @Value("${product.all}")
    private String productAll;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Product> productList = productMapper.findByAll();
        HashMap<String, Product> map = new HashMap<>();
        for (Product product: productList){
            map.put(product.getId().toString(), product);
        }
        redisTemplate.opsForHash().putAll(productAll, map);
    }
}
